<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <link rel="stylesheet" href="/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <div class="layui-form">
                <legend>搜索信息</legend>
                <div style="margin: 10px 10px 10px 10px">
                    <div class="layui-inline">
                        <label class="layui-form-label">角色名称</label>
                        <div class="layui-input-inline">
                            <input id="name" type="text" name="name" autocomplete="off" class="layui-input">
                        </div>
                    </div><!--
                    <div class="layui-inline">
                        <label class="layui-form-label">所属部门</label>
                        <div class="layui-input-block">
                            <select id="dept" name="deptId" lay-verify="aihao">
                                <option value=""></option>
                            </select>
                        </div>
                    </div>-->
                    <div class="layui-inline">
                        <button class="layui-btn layui-btn-primary" data-type="reloads" id="reloads"><i
                                class="layui-icon"></i> 搜 索
                        </button>
                    </div>
                </div>
            </div>
        </fieldset>

        <script type="text/html" id="toolbar">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="roleTableId" lay-filter="roleTableFilter"></table>

        <script type="text/html" id="roleTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs layui-btn-warm" lay-event="menu">菜单授权</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
    </div>
</div>
<script src="/static/lib/layui/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        //初始化表格加载
        var tableId = table.render({
            elem: '#roleTableId',
            url: '/sys/role/list',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print'],
            response: {
                statusCode: 200 //规定成功的状态码，默认：0
                , countName: 'total' //规定数据总数的字段名称，默认：count
            },
            cols: [
                [
                    {type: "checkbox", width: '5%'},
                    {field: 'name', width: '10%', title: '角色名称'},
                    {field: 'deptName', width: '10%', title: '所属部门'},
                    {field: 'description', width: '14%', title: '描述'},
                    {field: 'createDate', width: '10%', title: '创建时间', sort: true},
                    {title: '操作', minWidth: 150, toolbar: '#roleTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 20,
            page: true,
            id: 'tableId',
            skin: 'line'
        });

        //自定义方法
        var active = {
            //执行搜索重载
            reloads: function () {
                var name = $('#name');
                /*var dept = $('#dept');*/

                //执行重载
                tableId.reload({
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        name: name.val()
                        /*,deptId: dept.val()*/
                    }
                }, 'data');
            }/*,
            //搜索下拉框加载
            deptinit: function () {
                var dept = $('#dept');
                $.ajax({
                    type: "get",
                    url: "/sys/dept/list",
                    dataType: "json",
                    success: function (res) {
                        if (!res.success) return;
                        var data = res.data;
                        for (var i = 0; i < data.length; i++) {
                            dept.append('<option value="' + data[i].id + '">' + data[i].name + '</option>');
                        }
                        form.render('select'); //刷新select选择框渲染
                    }
                });
            }*/
        };

        //执行下拉框加载
        /*active.deptinit();*/

        /**
         * toolbar监听事件（新增和批量删除）
         */
        table.on('toolbar(roleTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var open = layer.open({
                    title: '添加角色信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/sys/role/show',
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];

                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            $.ajax({
                                type: "post",
                                url: "/sys/role/save",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg("错误,联系管理员! #" + data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('tableId')
                    , data = checkStatus.data;

                if (data.length <= 0) {
                    layer.msg("未选中任何行");
                    return;
                } else {
                    var ids = [];
                    ids[0] = data[0].id;
                    var names = data[0].name;
                    for (var i = 1; i < data.length; i++) {
                        ids[i] = data[i].id;
                        names += "," + data[i].name;
                    }
                    layer.confirm('确认删除:' + names, function (index) {
                        $.ajax({
                            type: "delete",
                            url: "/sys/role/delete",
                            dataType: "json",
                            traditional: true,
                            data: {"ids": ids},
                            success: function (data) {
                                layer.closeAll();
                                layer.msg(data.msg);
                                active.reloads();
                            },
                            error: function (data) {
                                layer.closeAll();
                                layer.msg(data);
                            }
                        });
                        layer.close(index);
                    });
                }
            }
        });

        //操作监听(修改、删除单个、授权)
        table.on('tool(roleTableFilter)', function (obj) {
            var data = obj.data;
            //修改用户
            if (obj.event === 'edit') {
                var open = layer.open({
                    title: '修改角色信息',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/sys/role/show?id=' + data.id,
                    yes: function (index, layero) {
                        var body = layer.getChildFrame('body', index);
                        //得到iframe页的窗口对象，执行iframe页的方法：iframeWin.method();
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        iframeWin.layui.form.on("submit(save)", function (data) {
                            var files = data.field;
                            files['id'] = obj.data.id;
                            $.ajax({
                                type: "put",
                                url: "/sys/role/edit",
                                dataType: "json",
                                data: files,
                                success: function (data) {
                                    layer.closeAll();
                                    layer.msg(data.msg);
                                    active.reloads();
                                },
                                error: function (data) {
                                    layer.closeAll();
                                    layer.msg(data);
                                }
                            });
                            return false;
                        });
                        body.find("#save").click();
                    }
                });
                return false;
            } else if (obj.event === 'delete') {        //删除单个用户
                layer.confirm('确认要删除:' + data.name, function (index) {
                    $.ajax({
                        type: "delete",
                        url: "/sys/role/delete",
                        dataType: "json",
                        data: {
                            "ids": data.id
                        },
                        success: function (data) {
                            layer.closeAll();
                            layer.msg(data.msg);
                            if (data.success) {
                                obj.del();
                            }
                        },
                        error: function (data) {
                            layer.closeAll();
                            layer.msg(data);
                        }
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'menu') {      //角色授权界面
                var open = layer.open({
                    title: '菜单授权',
                    type: 2,
                    shadeClose: true,
                    scrollbar: false,
                    area: ['30%', '60%'],
                    btn: ['确定', '取消'],
                    btnAlign: 'c',
                    content: '/sys/role/menu?id='+data.id,
                    yes: function (index, layero) {
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        var mdata = iframeWin.insTb.checkStatus();
                        var ids=[];
                        mdata.forEach(item=>{
                            ids.push(item.id);
                        });
                        $.ajax({
                            type: "post",
                            url: "/sys/role/menu",
                            dataType: "json",
                            traditional: true,
                            data: {
                                "id": data.id,
                                "ids": ids
                            },
                            success: function (data) {
                                layer.closeAll();
                                layer.msg(data.msg);
                            },
                            error: function (data) {
                                layer.closeAll();
                                layer.msg(data);
                            }
                        });
                        layer.close(index);
                    }
                });
            }
        });

        //加载自定义方法
        $('[data-type]').click(function () {
            var type = $(this).data('type');
            active[type].call(this)
        })

    })
    ;
</script>

</body>
</html>